<template>
  <div class="seachproduct">
    <van-dropdown-menu>
      <van-dropdown-item
        v-model="value1"
        :options="option1"
        title="综合"
        disabled
      />
      <van-dropdown-item
        v-model="value1"
        :options="option1"
        title="价格"
        @change="priceChange"
      />
      <van-dropdown-item
        v-model="value2" 
        :options="filterCategory"
        title="分类"
        @change="categoryChange"
      />
    </van-dropdown-menu>

    <!-- 数组为空时，显示 对不起，没有这类商品 -->
    <van-empty v-if="searchTipsProductList.length==0" description="对不起，没有这类商品" />
    <!-- 数组不为空时渲染 -->
    <Products v-else :searchTipsProductList="searchTipsProductList" />
  </div>
</template>

<script>
import Products from "../../components/Products";
export default {
  props: ["searchTipsProductList", "filterCategory"],  //searchTipsProductList商品列表
  components: {
    Products,
  },
  methods: {
    categoryChange(value) {
      console.log("选中了", value);
      this.$emit("categoryChange", value);
      // 其实value就是分类id
    },
    priceChange(value) {
      console.log("价格变了", value);
      this.$emit("priceChange", value);
    },
  },
  // 需求：正确的勾上分类中的某一项：要把value2做活了，value2的值必须是filterCategory这个数组中checked为true的这一项
  beforeUpdate() {
    // 遍历filterCategory中的每一项，把checked为true的这一项的value值，给到this.value2
    // filterCategory数组是
    // 0: { text: "全部", value: 0, checked: true }
    // 0: { text: "餐厨", value: 1005001, checked: false }
    this.filterCategory.map((item) => {
      if (item.checked) {
        this.value2 = item.value; //checked为true的这一项的value值，给到this.value2
      }
    });
  },
  data() {
    return {
      value1: "",   //价格初始值
      value2: "desc",  //分类初始值
      option1: [
        { text: "价格由高到低", value: "desc" },
        { text: "价格由低到高", value: "asc" },
      ],
      // option2: [
      //   { text: "全部", value: "a" },
      //   { text: "居家", value: "b" },
      // ],
    };
  },
};
</script>
 
<style lang = "less" scoped>
</style>